package com.jboltai.event.ai.aiintention;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jboltai.capability.message.AIIntention;
import com.jboltai.capability.message.AIMessageRole;
import com.jboltai.event.CompleteHandler;
import com.jboltai.event.EventType;
import com.jboltai.event.FailHandler;
import com.jboltai.event.SuccessHandler;
import com.jboltai.event.ai.aitext.AITextEvent;
import com.jboltai.event.handler.AIIntentionEventHandler;
import com.jboltai.resource.ai.AIModel;

import java.util.ArrayList;
import java.util.List;

/**
 * ai事件
 */
public class AIIntentionEvent extends AITextEvent<AIIntentionEvent, AIIntention, AIIntentionEventHandler> {

    private static final double DEFAULT_TEMPERATURE = 0.0D;


    public AIIntentionEvent() {
        super(EventType.AI_INTENTION);
        setTemperature(DEFAULT_TEMPERATURE);
    }

    public AIIntentionEvent(AIModel model) {
        super(EventType.AI_INTENTION);
        setTemperature(DEFAULT_TEMPERATURE);
        this.aiChatEvent.setModel(model);
    }


    /**
     * 创建一个ai事件
     * @param model 使用哪个模型，具体的模型名称根据该枚举值的defaultModel获取
     * @param successHandler
     * @param failHandler
     */
    public AIIntentionEvent(AIModel model, SuccessHandler<AIIntentionEvent, AIIntention> successHandler, FailHandler failHandler) {
        super(EventType.AI_INTENTION);
        setTemperature(DEFAULT_TEMPERATURE);
        this.aiChatEvent.setModel(model);
        this.successHandler = successHandler;
        this.failHandler = failHandler;
    }
    /**
     * 创建一个ai事件
     * @param model 使用哪个模型，具体的模型名称根据该枚举值的defaultModel获取
     * @param successHandler
     * @param failHandler
     */
    public AIIntentionEvent(AIModel model, SuccessHandler<AIIntentionEvent, AIIntention> successHandler, FailHandler failHandler, CompleteHandler completeHandler) {
        super(EventType.AI_INTENTION);
        setTemperature(DEFAULT_TEMPERATURE);
        this.aiChatEvent.setModel(model);
        this.successHandler = successHandler;
        this.failHandler = failHandler;
        this.completeHandler = completeHandler;
    }




    /**
     * 意图选项
     */
    private List<AIIntention> intentions;


    @Override
    public String preCheck() {
        if (StrUtil.isBlank(this.oriPrompt) &&
                (this.aiChatEvent == null || this.aiChatEvent.getContextMessages().isEmpty() || this.aiChatEvent.getContextMessages().stream().noneMatch(item -> item.getRole() == AIMessageRole.user))) {
            return "未设置用户消息";
        }
        if (this.intentions == null || this.intentions.isEmpty()) {
            return "未设置意图选项";
        }
        for (AIIntention intention : intentions) {
            if (StrUtil.isBlank(intention.getKey())) {
                throw new RuntimeException("意图的key未指定");
            }
            if (StrUtil.isBlank(intention.getDescription()) && (intention.getCases() == null || intention.getCases().length == 0)) {
                throw new RuntimeException("意图的描述和示例不可都为空");
            }
        }
        return null;
    }

    /**
     * 设置意图选项
     * @param intentions
     * @return
     */
    public AIIntentionEvent bindIntentionOptions(AIIntention... intentions) {
        if (intentions == null) {
            throw new IllegalArgumentException("意图选项不能为空");
        }
        if (this.intentions == null) {
            this.intentions = new ArrayList<>();
        }
        for (AIIntention intention : intentions) {
            this.intentions.add(intention);
        }

        return this;
    }


    public List<AIIntention> getIntentions() {
        return intentions;
    }

    /**
     * 获取function资源的json字符串
     * @return
     */
    public String getIntentionJSON() {
        if (intentions == null) return null;
        JSONArray array = new JSONArray();

        for (AIIntention intention : intentions) {
            array.add(
                    new JSONObject().fluentPut("key", intention.getKey()).fluentPut("description", intention.getDescription())
                            .fluentPut("cases", intention.getCases())
            );
        }

        return array.toJSONString();
    }


}
